package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Specialty;
import com.hs.sfzy.service.SpecialtyService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SpecialtyController
 * @Description TODO
 * @Author 李明辉
 * @Date 2024年10月24日 17:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/specialty")
public class SpecialtyController {
    @Autowired
    private SpecialtyService specialtyService;

    @GetMapping
    public Result selectAll(Specialty specialty){
        List<Specialty> specialties = specialtyService.selectAll(specialty);
        return Result.success(specialties);
    }

    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "5") int pageSize,
            Specialty specialty){
        PageInfo<Specialty> specialtyPageInfo = specialtyService.selectByPageInfo(pageNum, pageSize, specialty);
        return Result.success(specialtyPageInfo);
    }

    @PostMapping()
    public Result insert(@RequestBody Specialty specialty){
        int num = specialtyService.insert(specialty);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @PutMapping()
    public Result update(@RequestBody Specialty specialty){
        int num = specialtyService.update(specialty);
        if (num != 0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        int num = specialtyService.delete(id);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<Specialty> specialties = specialtyService.findAll();
        return Result.success(specialties);
    }
}
